package com.exercise.widgets.menu.widget

import android.content.Context
import android.util.AttributeSet
import android.view.ContextMenu
import android.view.View
import android.widget.AdapterView.AdapterContextMenuInfo
import androidx.recyclerview.widget.RecyclerView

/**
 * 带ContextMenuInfo的RecyclerView
 */
class RecyclerViewWithContextMenu : RecyclerView {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    private lateinit var contextMenuInfo: AdapterContextMenuInfo

    override fun getContextMenuInfo(): ContextMenu.ContextMenuInfo {
        return contextMenuInfo
    }

    override fun showContextMenuForChild(originalView: View, x: Float, y: Float): Boolean {
        val position = getChildLayoutPosition(originalView)
        val longId = getChildItemId(originalView)
        contextMenuInfo = AdapterContextMenuInfo(originalView, position, longId)
        return super.showContextMenuForChild(originalView, x, y)
    }
}